package com.zkk.service.impl;

import com.zkk.dao.DailyRecordDao;
import com.zkk.dao.impl.DailyRecordDaoImpl;
import com.zkk.pojo.DailyRecord;
import com.zkk.pojo.Page;
import com.zkk.service.DailyRecordService;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.List;

public class DailyRecordServiceImpl implements DailyRecordService {
    DailyRecordDao dailyRecordDao = new DailyRecordDaoImpl();
    @Override
    public int record(DailyRecord dailyRecord) {
        return dailyRecordDao.record(dailyRecord);
    }

    @Override
    public List<DailyRecord> showAll() {
        return dailyRecordDao.showAll();
    }

    @Override
    public Page<DailyRecord> showPage(Page<DailyRecord> page) {
        int currentPage = page.getCurrentPage();
        int pageSize = page.getPageSize();
        //获得当页数据
        List<DailyRecord> dailyRecords = dailyRecordDao.showPage((currentPage - 1) * pageSize, pageSize);
        page.setPageData(dailyRecords);
        //获得总数据个数
        int count = dailyRecordDao.recordSum();
        page.setRecordSum(count);
        //获得总页数
        int totalPage=(pageSize+count-1)/pageSize;
        page.setTotalPage(totalPage);


        //设置页码范围
        int beginPage = 0;
        int endPage = 0 ;
        if(totalPage<=5){
            beginPage = 1;
            endPage = 5;
        }else if(totalPage>=6){
            if(currentPage<=3){
                beginPage = 1;
                endPage = 5;
            }else if(currentPage>=4 & currentPage<=7){
                beginPage = currentPage - 2;
                endPage = currentPage + 2;
            }else {
                beginPage = totalPage - 4;
                endPage = totalPage;
            }
        }
        //设置首尾页
        page.setBeginPage(beginPage);
        page.setEndPage(endPage);
        return page;
    }
}
